package com.java110.community.cmd.ownerRepair;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairSettingDto;
import com.java110.dto.repair.RepairStaffsFlow;
import com.java110.dto.repair.RepairStateEnum;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairSettingInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.DateUtil;
import com.java110.vo.api.repair.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Java110Cmd(serviceCode = "ownerRepair.repairDispatch")
public class RepairDispatchCmd extends Cmd {
    @Autowired
    private IRepairInnerServiceSMO repairInnerService;
    @Autowired
    private IRepairSettingInnerServiceSMO repairSettingInnerService;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        Long repairId = reqJson.getLong("repairId");
        RepairDto repairDto = repairInnerService.getOneById(repairId);
        if ("BACK".equals(reqJson.getString("action"))) {
            //退单
            //查看派单方式
            Long repairType = repairDto.getRepairType();
            RepairSettingDto settingDto = repairSettingInnerService.getOneById(repairType);
            String repairWay = settingDto.getRepairWay();
            if ("200".equals(repairWay)) {
                reqJson.put("repairType",repairDto.getId());
                //指派-回到待派单状态
                //修改维修单状态
                reqJson.put("staffId", null);
                reqJson.put("staffName", null);
                reqJson.put("state", RepairStateEnum.WAITING.getState());
                reqJson.put("stateName", RepairStateEnum.WAITING.getStateName());
                repairInnerService.repairDispatch(reqJson);
                //修改流转单状态
                reqJson.put("repairType", repairDto.getRepairType());
                saveReapirFlow(reqJson, repairId);
                return;
            } else if ("300".equals(repairWay)) {
                //轮询-指派下一个
                //查找下一个维修师傅
                StaffVo staff = null;
                List<StaffVo> staffVos = repairSettingInnerService.listRepairTypeUsers(reqJson);
                Boolean tag = false;
                for (StaffVo staffVo : staffVos) {
                    if (staffVo.getStaffId().equals(repairDto.getStaffId())) {
                        tag = true;
                        continue;
                    }
                    if (tag) {
                        staff = staffVo;
                    }
                }
                if (staff != null) {
                    //匹配到师傅
                    //修改维修单状态
                    reqJson.put("staffId", staff.getStaffId());
                    reqJson.put("staffName", staff.getStaffName());
                    reqJson.put("state", RepairStateEnum.ORDER_TAKING.getState());
                    reqJson.put("stateName", RepairStateEnum.ORDER_TAKING.getStateName());
                }else {
                    //没有匹配到，修改为待派单
                    reqJson.put("staffId", null);
                    reqJson.put("staffName", null);
                    reqJson.put("state", RepairStateEnum.WAITING.getState());
                    reqJson.put("stateName", RepairStateEnum.WAITING.getStateName());
                }
                //修改流转单状态
                saveReapirFlow(reqJson, repairId);
                reqJson.put("repairType",repairDto.getId());
                repairInnerService.repairDispatch(reqJson);
                return;
            }else {
                //todo:100抢单-待完成
            }


        }
        //先查看当前订单是否已经被派发
        String staffId = reqJson.getString("staffId");
        String userId = reqJson.getString("userId");
        if (repairDto.getStaffId() != null && !repairDto.getStaffId().

                equals(userId)) {
            throw new CmdException("该订单已经派发，请勿重复派发");
        }
        //不能重复指派自己
        if (staffId.equals(repairDto.getStaffId())) {
            throw new CmdException("不能转给自己");
        }
        //指派维修师傅
        reqJson.put("state", RepairStateEnum.ORDER_TAKING.getState());
        reqJson.put("stateName", RepairStateEnum.ORDER_TAKING.getStateName());
        saveReapirFlow(reqJson, repairId);
        repairInnerService.repairDispatch(reqJson);
        //修改订单状态
        Long id = repairDto.getId();
        reqJson.put("id", id);
        reqJson.remove("context");
        repairInnerService.updateOwnerRepair(reqJson);
    }

    private void saveReapirFlow(JSONObject reqJson, Long repairId) {
        //查出上一个流程的订单
        RepairStaffsFlow repairStaffsFlows = repairInnerService.lastTimeRepairFlow(repairId);
        long time = repairStaffsFlows.getCreatTime().getTime();
        long now = System.currentTimeMillis();
        repairStaffsFlows.setEndTime(DateUtil.getCurrentDate());
        repairStaffsFlows.setTimeConsuming(Long.valueOf(now - time));
        repairInnerService.updateRepairDispatchFlow(repairStaffsFlows);
        //添加当前保修流程
        reqJson.put("createTime", DateUtil.getCurrentDate());
        reqJson.put("createUserName", reqJson.get("userName"));
        reqJson.put("currentUserId", reqJson.get("userId"));
        repairInnerService.repairDispatchFlow(reqJson);
    }
}
